import 'dart:convert';

import '../../../../data/models/auth/user.dart';
import '../../../exercises/exercises_page/models/exercise.dart';
import '../../../gyms/gyms_page/models/gym.dart';


class HomeModel {
  final String attendanceCount;
  final List<AttendanceDatum> attendanceData;
  final List<Exercise> exercise;
  final List<FluffyGym> gyms;
  final List<Gym> gym;
  final List<User> user;

  HomeModel({
    required this.attendanceCount,
    required this.attendanceData,
    required this.exercise,
    required this.gyms,
    required this.gym,
    required this.user,
  });

  HomeModel copyWith({
    String? attendanceCount,
    List<AttendanceDatum>? attendanceData,
    List<Exercise>? exercise,
    List<FluffyGym>? gyms,
    List<Gym>? gym,
    List<User>? user,
  }) =>
      HomeModel(
        attendanceCount: attendanceCount ?? this.attendanceCount,
        attendanceData: attendanceData ?? this.attendanceData,
        exercise: exercise ?? this.exercise,
        gyms: gyms ?? this.gyms,
        gym: gym ?? this.gym,
        user: user ?? this.user,
      );

  factory HomeModel.fromRawJson(String str) => HomeModel.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory HomeModel.fromJson(Map<String, dynamic> json) => HomeModel(
    attendanceCount: json["attendance_count"].toString(),
    attendanceData: List<AttendanceDatum>.from(json["attendance_data"].map((x) => AttendanceDatum.fromJson(x))),
    exercise: List<Exercise>.from(json["exercise"].map((x) => Exercise.fromJson(x))),
    gyms: List<FluffyGym>.from(json["gyms"].map((x) => FluffyGym.fromJson(x))),
    gym: List<Gym>.from(json["gym"].map((x) => Gym.fromJson(x))),
    user: List<User>.from(json["user"].map((x) => User.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "attendance_count": attendanceCount,
    "attendance_data": List<dynamic>.from(attendanceData.map((x) => x.toJson())),
    "exercise": List<dynamic>.from(exercise.map((x) => x.toJson())),
    "gyms": List<dynamic>.from(gyms.map((x) => x.toJson())),
    "gym": List<dynamic>.from(gym.map((x) => x.toJson())),
    "user": List<dynamic>.from(user.map((x) => x.toJson())),
  };
}

class AttendanceDatum {
  final String attendanceId;
  final String userId;
  final String exerciseId;
  final DateTime datetime;
  final String createdBy;
  final User user;
  final DateTime createAt;
  final DateTime updateAt;

  AttendanceDatum({
    required this.attendanceId,
    required this.userId,
    required this.exerciseId,
    required this.datetime,
    required this.createdBy,
    required this.user,
    required this.createAt,
    required this.updateAt,
  });

  AttendanceDatum copyWith({
    String? attendanceId,
    String? userId,
    String? exerciseId,
    DateTime? datetime,
    String? createdBy,
    User? user,
    DateTime? createAt,
    DateTime? updateAt,
  }) =>
      AttendanceDatum(
        attendanceId: attendanceId ?? this.attendanceId,
        userId: userId ?? this.userId,
        exerciseId: exerciseId ?? this.exerciseId,
        datetime: datetime ?? this.datetime,
        createdBy: createdBy ?? this.createdBy,
        user: user ?? this.user,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
      );

  factory AttendanceDatum.fromRawJson(String str) => AttendanceDatum.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory AttendanceDatum.fromJson(Map<String, dynamic> json) => AttendanceDatum(
    attendanceId: json["attendance_id"].toString(),
    userId: json["user_id"].toString(),
    exerciseId: json["exercise_id"].toString(),
    datetime: DateTime.parse(json["datetime"].toString()),
    createdBy: json["created_by"].toString(),
    user: User.fromJson(json["user"]),
    createAt: DateTime.parse(json["create_at"].toString()),
    updateAt: DateTime.parse(json["update_at"].toString()),
  );

  Map<String, dynamic> toJson() => {
    "attendance_id": attendanceId,
    "user_id": userId,
    "exercise_id": exerciseId,
    "datetime": datetime.toIso8601String(),
    "created_by": createdBy,
    "user": user.toJson(),
    "create_at": createAt.toIso8601String(),
    "update_at": updateAt.toIso8601String(),
  };
}

class AttendanceDatumUser {
  final String level;
  final String active;
  final String fullName;
  final String? image;

  AttendanceDatumUser({
    required this.level,
    required this.active,
    required this.fullName,
    this.image,
  });

  AttendanceDatumUser copyWith({
    String? level,
    String? active,
    String? fullName,
    String? image,
  }) =>
      AttendanceDatumUser(
        level: level ?? this.level,
        active: active ?? this.active,
        fullName: fullName ?? this.fullName,
        image: image ?? this.image,
      );

  factory AttendanceDatumUser.fromRawJson(String str) => AttendanceDatumUser.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory AttendanceDatumUser.fromJson(Map<String, dynamic> json) => AttendanceDatumUser(
    level: json["level"].toString(),
    active: json["active"].toString(),
    fullName: json["full_name"].toString(),
    image: json["image"]== null ? null:(json["image"].toString().isEmpty?null:json["image"].toString()),
  );

  Map<String, dynamic> toJson() => {
    "level": level,
    "active": active,
    "full_name": fullName,
    "image": image,
  };
}


class FluffyGym {
  final String gymId;
  final String name;
  final List<GymExercise> exercises;

  FluffyGym({
    required this.gymId,
    required this.name,
    required this.exercises,
  });

  FluffyGym copyWith({
    String? gymId,
    String? name,
    List<GymExercise>? exercises,
  }) =>
      FluffyGym(
        gymId: gymId ?? this.gymId,
        name: name ?? this.name,
        exercises: exercises ?? this.exercises,
      );

  factory FluffyGym.fromRawJson(String str) => FluffyGym.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory FluffyGym.fromJson(Map<String, dynamic> json) => FluffyGym(
    gymId: json["gym_id"].toString(),
    name: json["name"].toString(),
    exercises: List<GymExercise>.from(json["exercises"].map((x) => GymExercise.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "gym_id": gymId,
    "name": name,
    "exercises": List<dynamic>.from(exercises.map((x) => x.toJson())),
  };
}

class GymExercise {
  final String exerciseId;
  final String name;

  GymExercise({
    required this.exerciseId,
    required this.name,
  });

  GymExercise copyWith({
    String? exerciseId,
    String? name,
  }) =>
      GymExercise(
        exerciseId: exerciseId ?? this.exerciseId,
        name: name ?? this.name,
      );

  factory GymExercise.fromRawJson(String str) => GymExercise.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GymExercise.fromJson(Map<String, dynamic> json) => GymExercise(
    exerciseId: json["exercise_id"].toString(),
    name: json["name"].toString(),
  );

  Map<String, dynamic> toJson() => {
    "exercise_id": exerciseId,
    "name": name,
  };
}

